package com.yn.yzq.controller;

import com.yn.yzq.service.LoginService;
import com.yn.yzq.utils.DataUtils;
import com.yn.yzq.utils.MyMD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/loginAbout")
public class LoginController {
    private final Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/loginVerify", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> UserLogin(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> returnInfo = new HashMap<String, Object>();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        logger.info("获取日志：" + request + username + password);
        if (username == null || (username.trim()).equals("") || password == null || (password.trim()).equals("")) {
            returnInfo.put("error", "错误代码：65841312");
            returnInfo.put("errorMSG", "输入为空！");
            return returnInfo;
        }

        map.put("username", username);
        map.put("password", MyMD5Util.getMD5String(password));

        Map<String, Object> getInfo = loginService.loginValid(map);
        if (getInfo == null || getInfo.equals("")) {
            returnInfo.put("code", "6636");
            returnInfo.put("errorMSG", "对不起，系统查询无此人!");
            return returnInfo;
        } else {
            returnInfo.put("code", "1");
            returnInfo.put("userInfo", getInfo);
            return returnInfo;
        }

    }

    @RequestMapping("/registerUser")
    @ResponseBody
    public Map<String, Object> registerUser(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> returnInfo = new HashMap<String, Object>();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String emali = request.getParameter("emali");
//        String created = request.getParameter("created");
//        String updated = request.getParameter("updated");

        if (username == null || (username.trim()).equals("")
                || password == null || (password.trim()).equals("")
                || phone == null || (phone.trim()).equals("")
                || emali == null || (emali.trim()).equals("")) {
            returnInfo.put("error", "错误代码：9999");
            returnInfo.put("errorMSG", "输入为空！");
            return returnInfo;
        }
        map.put("username", username);
        map.put("phone", phone);
        map.put("emali", emali);
        map.put("created", DataUtils.getDataNow());
        map.put("password", MyMD5Util.getMD5String(password));
        boolean registerUserFlage = loginService.registerUser(map);
        if (registerUserFlage) {
            returnInfo.put("successCode", "0000");
            returnInfo.put("successMSG", "注册成功!");
            return returnInfo;
        } else {
            returnInfo.put("error", "错误代码：6636");
            returnInfo.put("errorMSG", "输入为空！");
            return returnInfo;
        }

    }
}
